<!doctype html>
<html>
<head>
<title>XMLHttpRequest: abort() while sending data</title>
<script src="../resources/testharness.js"></script>
<script src="../resources/testharnessreport.js"></script>
</head>
<body>
Correct progress event sequencing on abort(), <a href="http://crbug.com/315488">bug 315488</a>.
<script>
// Based on http://w3c-test.org/xhr/abort-during-upload.htm

var expected = [
    'upload', 'abort',
    'upload', 'loadend',
    'load', 'abort',
    'load', 'loadend'];

var result = [];
function recordEvent(e) {
    var kind = e.target instanceof XMLHttpRequest ? 'load' : 'upload';
    result.push(kind, e.type);
}

var abortAfterSendLoadend = async_test("Progress event delivery on abort(), post-send() (async)");
abortAfterSendLoadend.step(function() {
    var xhr = new XMLHttpRequest();
    xhr.open("POST", "resources/delay.php?iteration=1&delay=1000");
    xhr.onprogress = recordEvent;
    xhr.onabort = recordEvent;
    xhr.onloadend = abortAfterSendLoadend.step_func(function (e) {
        recordEvent(e);
        assert_equals(xhr.readyState, XMLHttpRequest.DONE);
        assert_array_equals(result, expected);
        // Runs after abort()'s transition of readyState to UNSENT.
        setTimeout(abortAfterSendLoadend.step_func(function () {
            assert_equals(xhr.readyState, XMLHttpRequest.UNSENT);
            abortAfterSendLoadend.done();
        }), 0);
    });
    xhr.upload.onprogress = recordEvent;
    xhr.upload.onabort = recordEvent;
    xhr.upload.onloadend = recordEvent;

    xhr.send();
    xhr.abort();
});

var resultSync = [];
function recordEventSync(e) {
    var kind = e.target instanceof XMLHttpRequest ? 'load' : 'upload';
    resultSync.push(kind, e.type);
}
var abortAfterSendSync = async_test("Progress event delivery on abort(), post-send() (sync)");
abortAfterSendSync.step(function() {
    var xhr = new XMLHttpRequest();
    xhr.open("POST", "resources/delay.php?iteration=1&delay=10");
    xhr.onreadystatechange = function () {
        if (xhr.readyState > XMLHttpRequest.OPENED && xhr.readyState != XMLHttpRequest.DONE)
            xhr.abort();
    };
    xhr.onprogress = recordEventSync;
    xhr.onabort = recordEventSync;
    xhr.onloadend = abortAfterSendSync.step_func(function (e) {
        recordEventSync(e);
        assert_equals(xhr.readyState, XMLHttpRequest.DONE);
        assert_array_equals(resultSync, expected);
        // Runs after abort()'s transition of readyState to UNSENT.
        setTimeout(abortAfterSendSync.step_func(function () {
            assert_equals(xhr.readyState, XMLHttpRequest.UNSENT);
            abortAfterSendSync.done();
        }), 0);
    });
    xhr.upload.onprogress = recordEventSync;
    xhr.upload.onabort = recordEventSync;
    xhr.upload.onloadend = recordEventSync;
    xhr.send();
});
</script>
</body>
</html>
